import os
import subprocess

_version = (0, 1, 0)

def getVersionTuple():
    """
    Return current version as tuple.

    Tuples' elemets are:
        first -- major
        second -- minor
        third -- maintenance
    """
    return _version

def getVersionString():
    """Return version number as string"""

    return "%02i.%02i.%02i" % getVersionTuple()

def getRepoInfoString():
    """Return repository info as raw string."""

    _curDir = os.path.abspath(os.path.dirname(__file__))
    _p = subprocess.Popen(
        'svn info "%s"' % _curDir, stdout=subprocess.PIPE, shell=True)
    _p.wait()
    return _p.stdout.read()

def getPublicRepoData():
    """Return repository data with some fields stripped.

    It's just that not everyone would like to share their UUID and path.

    """
    _bannedLines = ("Path:", "Repository UUID:", "Node Kind:")
    return "\n".join(_line
        for _line in getRepoInfoString().splitlines()
        if not _line.startswith(_bannedLines))


def getFullData():
    _out = "=== Declared version ===\n%s\n\n" % getVersionString()
    _out += "=== Svn data ===\n" + getPublicRepoData()
    return _out


usage = """
    Command line interfaces:
        --get_repo_data : print reposetory data to stdout,
        --get_version_string : print version string to stdout,
        --get_full_data : print full data (repo & version)
"""

if __name__ == "__main__":
    """Provide some command-line interfaces"""

    import sys
    from getopt import getopt

    (_args, _) = getopt(sys.argv[1:], (), (
        "get_repo_data", "get_version_string", "get_full_data"))
    _args = dict(_args)
    if _args.has_key("--get_repo_data"):
        sys.stdout.write(getPublicRepoData())
    if _args.has_key("--get_version_string"):
        sys.stdout.write(getVersionString())
    if _args.has_key("--get_full_data"):
        sys.stdout.write(getFullData())

# vim: set sts=4 sw=4 et :
